package com.leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No01
 * @description 两数之和
 * @date 2021/1/25 下午4:22
 */
public class No01 {
    public static void main(String[] args) {
        int[] nums = {3,2,4};
        int target = 7;
        int[] res = twoSum(nums,target);
        System.out.println(Arrays.toString(res));
    }

    // 两数之和
    public static int[] twoSum(int[] nums, int target) {
        int[] res = {-1,-1};
        if(nums == null || nums.length < 1){
            return res;
        }
        Map<Integer,Integer> map = new HashMap<>();
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            if(map.containsKey(target - nums[i])){
                res[0] = i;
                res[1] = map.get(target - nums[i]);
            }else{
                map.put(nums[i],i);
            }

        }
        return res;
    }



}
